#ifndef ORDER_LINK_H_
#define ORDER_LINK_H_

#include <map>
#include <list>
#include "zmessage.h"
#include "adaptorsocketbase.h"
#include "PersistFile.h"
#include "AdaptorDlgBase.h"

using namespace std;

struct AdaptorOrder {
	ZAdaptorOrderMessage		mOrder;
	ZAdaptorOrderStatusMessage	mStatus;
	AdaptorSocketBase			*mSocket;
};

typedef pair<int, AdaptorOrder *> IDOrderPair;

class OrderLink
{
public:
	OrderLink(void);
	~OrderLink(void);
	virtual void Connect() {}
	virtual void Disconnect() {}

	bool LoadPersist();

	void managerOrder(AdaptorOrder *);
	AdaptorOrder* getOrder();
	void recycleOrder(AdaptorOrder *);
	virtual AdaptorOrder *newOrder() {return new AdaptorOrder();}

	void SetDialog(AdaptorDlgBase *dlg) {mDialog = dlg;}
protected:
	virtual void procPersistedLine(const string & line){}
	virtual bool sendOrder(AdaptorOrder *) {return false;}
	virtual bool cancelOrder(AdaptorOrder*, bool isCxl) {return false;}


	virtual bool persistOrder(AdaptorOrder *) {return false;}

	AdaptorOrder *lookupOrder(int orderID);
	void internalReject(AdaptorOrder *, ZAdaptorErrorCodeField::ErrorCode);
	void OnOrderAccepted(int orderID);
	void OnOrderClosed(int orderID);
	void OnOrderTraded(int orderID, double px, int sz);

	static void ProcPersistedLine(const string & line, void *ctx);
protected:
	bool	mLinkOk;
	map<int, AdaptorOrder *>	mOrderMap;
	list<AdaptorOrder *>		mFreeOrderList;

	PersistFile					*mPersistFile;

	AdaptorDlgBase				*mDialog;
};

#endif //ORDER_LINK_H_